/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.eb.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.eb.entity.OrderProduct;
import com.kaaction.modules.eb.service.OrderProductService;

/**
 * 订单与商品Controller
 * @author zengbiao
 * @version 2017-04-19
 */
@Controller
@RequestMapping(value = "${adminPath}/eb/orderProduct")
public class OrderProductController extends BaseController {

	@Autowired
	private OrderProductService orderProductService;
	
	@ModelAttribute
	public OrderProduct get(@RequestParam(required=false) String id) {
		OrderProduct entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = orderProductService.get(id);
		}
		if (entity == null){
			entity = new OrderProduct();
		}
		return entity;
	}
	
	@RequiresPermissions("eb:orderProduct:view")
	@RequestMapping(value = {"list", ""})
	public String list(OrderProduct orderProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OrderProduct> page = orderProductService.findPage(new Page<OrderProduct>(request, response), orderProduct); 
		model.addAttribute("page", page);
		return "modules/eb/orderProductList";
	}

	@RequiresPermissions("eb:orderProduct:view")
	@RequestMapping(value = "form")
	public String form(OrderProduct orderProduct, Model model) {
		model.addAttribute("orderProduct", orderProduct);
		return "modules/eb/orderProductForm";
	}

	@RequiresPermissions("eb:orderProduct:edit")
	@RequestMapping(value = "save")
	public String save(OrderProduct orderProduct, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, orderProduct)){
			return form(orderProduct, model);
		}
		orderProductService.save(orderProduct);
		addMessage(redirectAttributes, "保存订单与商品成功");
		return "redirect:"+Global.getAdminPath()+"/eb/orderProduct/?repage";
	}
	
	@RequiresPermissions("eb:orderProduct:edit")
	@RequestMapping(value = "delete")
	public String delete(OrderProduct orderProduct, RedirectAttributes redirectAttributes) {
		orderProductService.delete(orderProduct);
		addMessage(redirectAttributes, "删除订单与商品成功");
		return "redirect:"+Global.getAdminPath()+"/eb/orderProduct/?repage";
	}

}